﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace win1
{
    public partial class Form1 : Form
    {
        private DataTable dt = null;
        public Form1()
        {
            InitializeComponent();
        }
        //窗体加载事件
        private void Form1_Load(object sender, EventArgs e)
        {
            this.dataGridView1.AutoGenerateColumns = false;
            DataGridView();
        }

        private void DataGridView()
        {
            //准备SQL语句，调用封装的方法，设置数据源
            string sql = "select * from [user]";
            if (dt != null)
            {
                dt.Clear();
            }
             dt = DataBase.ExecutQ(sql, "[user]");
            //给dataGridView1设置数据源
            this.dataGridView1.DataSource = dt;
        }

        //添加
        private void btn1_Click(object sender, EventArgs e)
        {
            add ad = new add();
            ad.ShowDialog();
            DataGridView();
        }

        //删除
        private void btn3_Click(object sender, EventArgs e)
        {
            int tr = (int)this.dataGridView1.CurrentRow.Cells[0].Value;
            DialogResult result = MessageBox.Show("确定删除商品编号为    " + tr + "    的商品嘛？", "温馨提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);
            if (result == DialogResult.OK)
            {
                string sql = "delete from [user] where id =  " + tr + "  ";
                int num = DataBase.ExecuteU(sql);
                if (num > 0)
                {
                    MessageBox.Show("商品删除成功！", "操作提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                    //调用查询方法。
                    DataGridView();
                }
                else
                {
                    MessageBox.Show("商品删除失败", "操作提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                }
            }
        }

        //修改
        private void btn2_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.SelectedCells.Count == 1)
            {
                int id = Convert.ToInt32(this.dataGridView1.CurrentRow.Cells["id"].Value);
                Update update = new Update();
                update.id = id;
                update.ShowDialog();
                DataGridView();
            }
            else
            {
                MessageBox.Show("您至少要选中一行", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        //点击按钮进行查询
        private void button1_Click(object sender, EventArgs e)
        {
            string sql = string.Format("select * from [user] where username like '%{0}%';", this.textBox1.Text.Trim());
            if (dt != null)
            {
                dt.Clear();
            }
            dt = DataBase.ExecutQ(sql, "[user]");
            //给dataGridView1设置数据源
            this.dataGridView1.DataSource = dt;
        }
    }
}
